package com.work.intelligent_transportation2.fragment;

import android.app.Fragment;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.work.intelligent_transportation2.R;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import Api.Environment_api;
import Api.TrafficLIghts_api;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class TrafficLightsSystemFragment extends Fragment {
    private RecyclerView recycle;
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private OkHttpClient okHttp = new OkHttpClient();
    private String mParam1;
    private String mParam2;
    private Spinner spList;
    private TextView textView9;
    private TextView textView10;
    private Button button2;
    private List<String> list = new ArrayList<String>();
    private List<TrafficLIghts_api> Traffic_list = new ArrayList<>();
    private ArrayAdapter<String> arr_adapter;
    public TrafficLightsSystemFragment() {
    }

    public static TrafficLightsSystemFragment newInstance(String param1, String param2) {
        TrafficLightsSystemFragment fragment = new TrafficLightsSystemFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        String url = "http://121.9.253.237:8888/traffic/api/v2/get_trafficlight_config";
        new Thread(new Runnable() {
            @Override
            public void run() {
                JsonObject jsonObject = new JsonObject();
                for (int x=1;x<=5;x++){
                    if (x==3){
                 jsonObject.addProperty("TrafficLightId", x);
                    }else {
                 jsonObject.addProperty("TrafficLightId", x);
                    }
                if (x<2){
                    jsonObject.addProperty("UserName", "user2");
                }else {
                    jsonObject.addProperty("UserName", "user1");
                }
                String json = jsonObject.toString();
//MediaType对象包含了三种信息：type 、subtype以及charset，一般将这些信息传入parse()方法中，这样就可以解析出MediaType对象，比如 “text/x-markdown; charset=utf-8”
                MediaType Medipet = MediaType.Companion.parse("application/json;charset=utf-8");
                RequestBody requestBody = RequestBody.create(json, Medipet);
                Request request = new Request.Builder().url(url).post(requestBody).build();
                Call call = okHttp.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                    }
                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        String result = response.body().string();
                        Log.i("tag_2", result);
                        Gson gson = new Gson();
                        TrafficLIghts_api loginBean = gson.fromJson(result, TrafficLIghts_api.class);
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getActivity());
        recycle.setLayoutManager(linearLayoutManager);
        String Y=String.valueOf(1);
        String rad=String.valueOf(loginBean.getRedTime());
        String green=String.valueOf(loginBean.getGreenTime());
        String Yollow=String.valueOf(loginBean.getYellowTime());
        try{
            Traffic_list.add(new TrafficLIghts_api(Y,rad,green,Yollow));
        } catch (Exception e){
        }
        TrafficLights_Apadt trafficLights_apadt=new TrafficLights_Apadt(Traffic_list);
        recycle.setAdapter(trafficLights_apadt);
                            }
                        });
                    }
                });
                }
            }
        }).start();
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_traffic_lights_system, container, false);
        spList = view.findViewById(R.id.sp_list);
        textView9 =view.findViewById(R.id.textView9);
        textView10 = view. findViewById(R.id.textView10);
        button2 = view. findViewById(R.id.button2);
        recycle=view.findViewById(R.id.recyle);
        list = new ArrayList<String>();
        list.add("路口升序");
        list.add("路口降序");
        list.add("红灯升序");
        list.add("红灯降序");
        list.add("绿灯升序");
        list.add("绿灯降序");
        list.add("黄灯升序");
        list.add("黄灯降序");
        arr_adapter= new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, list);
        arr_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spList.setAdapter(arr_adapter);
        spList.setSelection(0,true);
        spList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            //监听
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String date=adapterView.getItemAtPosition(i).toString();
                switch (date){
                    case "路口升序":
                        Toast.makeText(getActivity(), "1", Toast.LENGTH_SHORT).show();
                        break;
                    case "路口降序":
                        Toast.makeText(getActivity(), "2", Toast.LENGTH_SHORT).show();
                        break;
                    case "红灯升序":
                        Toast.makeText(getActivity(), "3", Toast.LENGTH_SHORT).show();
                        break;
                    case "红灯降序":
                        Toast.makeText(getActivity(), "4", Toast.LENGTH_SHORT).show();
                        break;
                    case "绿灯升序":
                        Toast.makeText(getActivity(), "5", Toast.LENGTH_SHORT).show();
                        break;
                    case "绿灯降序":
                        Toast.makeText(getActivity(), "6", Toast.LENGTH_SHORT).show();
                        break;
                    case "黄灯升序":
                        Toast.makeText(getActivity(), "7", Toast.LENGTH_SHORT).show();
                        break;
                    case "黄灯降序":
                        Toast.makeText(getActivity(), "8", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        return view;
    }

}